package com.zy.dao.util;


import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.util.StringUtils;

import tk.mybatis.mapper.entity.Example;

import com.zy.dao.bs.CustomerClubDao;
import com.zy.dao.crm.CustomerDao;
import com.zy.entity.bs.CustomerClub;
import com.zy.entity.crm.Customer;
import com.zy.entity.sys.User;
import com.zy.pojo.constants.Constant;
import com.zy.utils.ApplicationUtil;
import com.zy.utils.SessionUtil;

public class DataPermitUtil {

	private static CustomerClubDao CUSTOMECLUBRDAO;
	private static CustomerDao CUSTOMERDAO;

	private DataPermitUtil() {
	}

	static {
		CUSTOMECLUBRDAO = (CustomerClubDao) ApplicationUtil.getBean(CustomerClubDao.class);
		CUSTOMERDAO= (CustomerDao) ApplicationUtil.getBean(CustomerDao.class);
	}



	public static String getClubCustomer(String columnName) {
		Subject subject = SecurityUtils.getSubject();
		boolean isAdmin = subject.hasRole(Constant.ADMIN);
		if (isAdmin) {
			return "";
		}
		if(Objects.isNull((User) SessionUtil.getSessionAttribute(Constant.CURRENT_USER))){
			return "";
		}
		long user = ((User) SessionUtil.getSessionAttribute(Constant.CURRENT_USER)).getId();
		Customer c = new Customer();
		c.setUserId(user);
		c = CUSTOMERDAO.get(c);
		if(Objects.isNull(c)){
			return "";
		}
		Long customerId =c.getId();
		Example ex = new Example(CustomerClub.class);
		ex.createCriteria().andEqualTo("customerId",customerId);
		List<CustomerClub> cc = CUSTOMECLUBRDAO.listByExample(ex);
		List<Long> ids =  cc.stream().map(CustomerClub::getClubId).collect(Collectors.toList());
		StringBuilder sql = new StringBuilder(" and ");
		sql.append(columnName);
		sql.append(" in ( ");
		sql.append(StringUtils.collectionToDelimitedString(ids,","));
		sql.append(")");
		return sql.toString();
	}

//	public static String getTeamSql(String columnName, String teamIds) {
//		Subject subject = SecurityUtils.getSubject();
//		boolean isAdmin = subject.hasRole(Constant.ADMIN);
//		boolean isBlank = StringUtils.isBlank(teamIds);
//		if (isAdmin && isBlank) {
//			return "";
//		}
//		String allowTeamIds = getAllowTeamIds(teamIds, subject, isAdmin, isBlank);
//		StringBuilder sql = new StringBuilder(" and ");
//		sql.append(columnName);
//		sql.append(" in (");
//		sql.append(allowTeamIds);
//		sql.append(") ");
//		return sql.toString();
//	}
//
//	private static String getAllowTeamIds(String teamIds, Subject subject, boolean isAdmin, boolean isBlank) {
//		Set<Long> teamAllowIds = new HashSet<Long>();
//		Team root = new Team();
//		if (isAdmin) {
//			root.setId(Constant.ROOT);
//		} else {
//			long userId = ((User) SessionUtil.getSessionAttribute(Constant.CURRENT_USER)).getId();
//			long teamId = TEAMDAO.getTeamIdByUserId(userId);
//			root.setId(teamId);
//			teamAllowIds.add(teamId);
//		}
//		appendNode(root, TEAMDAO.listAll(), teamAllowIds);
//		if (isBlank) {
//			return StringUtils.join(teamAllowIds, ",");
//		}
//		// 取交集
//		teamAllowIds.retainAll(ConvertUtil.convertSet(teamIds.split(",")));
//		return StringUtils.join(teamAllowIds, ",");
//	}
//
//	private static void appendNode(Team root, List<Team> teams, Set<Long> teamAllowIds) {
//		for (Team node : teams) {
//			if (node.getParentId().longValue() == root.getId()) {
//				teamAllowIds.add(node.getId());
//				appendNode(node, teams, teamAllowIds);
//			}
//		}
//	}

	@SuppressWarnings("unused")
	private static List<Long> conversionList(List<CustomerClub> cc){
		List<Long> li = new ArrayList<>();
	    cc.stream().collect(Collectors.toList());
        return li;
	}

}
